package com.yungu.swift.api.route.v2.driver;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.miemiedev.mybatis.paginator.domain.PageList;
import com.yungu.swift.api.base.BaseApi;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.base.model.annotation.ResponseImgOverride;
import com.yungu.swift.constants.CommonConstant;
import com.yungu.swift.constants.OrderConstant;
import com.yungu.swift.order.model.param.CrosstownAssignOrderParam;
import com.yungu.swift.order.model.param.CrosstownOrderParam;
import com.yungu.swift.order.model.param.MergeAssignParam;
import com.yungu.swift.order.model.vo.*;
import com.yungu.swift.order.service.OrderAssignService;
import com.yungu.swift.order.service.OrderJoinService;
import com.yungu.swift.utils.JsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value = "/api/v2/driver/")
@Api(value = "【司机】【订单】司机跨城订单操作相关接口", tags = "司机跨城订单相关")
public class DriCrosstownOrderApi extends BaseApi {

    @Reference
    private OrderJoinService orderJoinService;

    @Reference
    private OrderAssignService orderAssignService;

    @ResponseImgOverride
    @RequestMapping(value = {"/token/crosstown/order/stateChange"})
    @ApiOperation(value = "拼车&小件状态变更", response = ResponseData.class)
    public ResponseData<Map<String, Object>> stateChange(@RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        crosstownOrderParam.setAppid(appid);
        log.info("订单状态变更1{}", JsonUtils.toJSONString(crosstownOrderParam));
        return orderJoinService.crosstownOrderStateChange(crosstownOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/homeOrderInfo"})
    @ApiOperation(value = "跨城司机获取主页订单信息（待出发订单）", response = ResponseData.class)
    public ResponseData<PageList<CrosstownOrderVo>> stateChange(
            HttpServletRequest request, @RequestHeader String appid) {

        CrosstownOrderParam crosstownOrderParam = new CrosstownOrderParam();
        crosstownOrderParam.setAppid(appid);
        crosstownOrderParam.setDriverUuid(getUserUid(request));
        crosstownOrderParam.setCloseStatus(OrderConstant.ORDER_CLOSE_STATUS_NORMAL);
        //等待出发状态 与行程开始状态
        crosstownOrderParam.setJoinStatusList(Arrays.asList(OrderConstant.ORDER_JOIN_STATUS_WAIT_START));
        return orderJoinService.crosstownOrderListInfo(crosstownOrderParam, 0);
    }

    @RequestMapping(value = {"/token/crosstown/order/listInfo"})
    @ApiOperation(value = "跨城司机获取所有订单列表", response = ResponseData.class)
    public ResponseData<PageList<CrosstownOrderVo>> crosstownOrderListInfo(
            HttpServletRequest request, @RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        crosstownOrderParam.setDriverUuid(getUserUid(request));
        crosstownOrderParam.setAppid(appid);
        return orderJoinService.crosstownOrderListInfo(crosstownOrderParam, 0);
    }

    @ResponseImgOverride
    @RequestMapping(value = {"/token/crosstown/order/listDetail"})
    @ApiOperation(value = "跨城司机获取订单详情", response = ResponseData.class)
    public ResponseData<CrosstownDriverOrderDetailVo> listInfo(@RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        crosstownOrderParam.setAppid(appid);
        return orderJoinService.crosstownOrderListDetail(crosstownOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/sequenceChange"})
    @ApiOperation(value = "跨城司机更新订单顺序", response = ResponseData.class)
    public ResponseData<Boolean> orderSequenceChange(@RequestHeader String appid, @RequestBody Map<String, List<CrosstownOrderParam>> paraMap) {
        List<CrosstownOrderParam> list = paraMap.get("orderList");
        list.get(0).setAppid(appid);
        return orderJoinService.orderSequenceChange(list);
    }

    @RequestMapping(value = {"/token/crosstown/order/upderway"})
    @ApiOperation(value = "跨城司机获取进行中订单", response = ResponseData.class)
    public ResponseData<PageList<CrosstownOrderVo>> upderway(HttpServletRequest request, @RequestHeader String appid) {
        CrosstownOrderParam crosstownOrderParam = new CrosstownOrderParam();
        crosstownOrderParam.setAppid(appid);
        String driverUuid = getUserUid(request);
        crosstownOrderParam.setDriverUuid(driverUuid);
        crosstownOrderParam.setCloseStatus(OrderConstant.ORDER_CLOSE_STATUS_NORMAL);
        //进行中订单包含司机未支付
        crosstownOrderParam.setDriverNoPay(1);
        //行程开始与行程中
        crosstownOrderParam.setJoinStatusList(Arrays.asList(OrderConstant.ORDER_JOIN_STATUS_DRIVER_DEPARTURE, OrderConstant.ORDER_JOIN_STATUS_STARTED));
        return orderJoinService.crosstownOrderListInfo(crosstownOrderParam, 0);
    }

    @RequestMapping(value = {"/token/crosstown/order/grabOrder"})
    @ApiOperation(value = "跨城司机获取进行中订单", response = ResponseData.class)
    public ResponseData<JsonOrderDetailVo> grabOrder(
            HttpServletRequest request, @RequestHeader String appid, @RequestBody Map<String, Object> paraMap) {
        //基础参数校验
        if (paraMap.get("orderUuid") == null) {
            return ResponseData.buildErrorResponse(ResponseData.ERROR_CODE, "校验失败，订单UUID为空");
        }
        String driverUuid = getUserUid(request);
        CrosstownAssignOrderParam crosstownAssignOrderParam = new CrosstownAssignOrderParam();
        crosstownAssignOrderParam.setTargetDriverUuid(driverUuid);
        crosstownAssignOrderParam.setCurrentOrderUuid(paraMap.get("orderUuid").toString());
        return orderAssignService.grabCrosstownOrder(crosstownAssignOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/poolInfo"})
    @ApiOperation(value = "跨城司机获取订单池", response = ResponseData.class)
    public ResponseData<PageList<CrosstownOrderPoolInfoVo>> poolInfo(
            HttpServletRequest request, @RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        String driverUuid = getUserUid(request);
        crosstownOrderParam.setDriverUuid(driverUuid);
        return orderJoinService.crosstownOrderPoolInfo(crosstownOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/poolCount"})
    @ApiOperation(value = "跨城司机获取订单池", response = ResponseData.class)
    public ResponseData<Map<String, Object>> poolCount(
            HttpServletRequest request, @RequestHeader String appid) {
        String driverUuid = getUserUid(request);
        CrosstownOrderParam crosstownOrderParam = new CrosstownOrderParam();
        crosstownOrderParam.setAppid(appid);
        crosstownOrderParam.setDriverUuid(driverUuid);
        return orderJoinService.crosstownOrderPoolCount(crosstownOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/confirmFare"})
    @ApiOperation(value = "确认费用", response = ResponseData.class)
    public ResponseData<Map<String, Object>> confirmFare(@RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        crosstownOrderParam.setAppid(appid);
        //选择乘客支付
        if (crosstownOrderParam.getPayer() == CommonConstant.USER_IDENTITY_PASSENGER) {
            crosstownOrderParam.setJoinStatus(OrderConstant.ORDER_JOIN_STATUS_PASSENGER_PAY);
        } else {
            //选择司机支付
            crosstownOrderParam.setJoinStatus(OrderConstant.ORDER_JOIN_STATUS_DRIVER_PAY);
        }
        log.info("订单状态变更2{}", JsonUtils.toJSONString(crosstownOrderParam));
        return orderJoinService.crosstownOrderStateChange(crosstownOrderParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/mergeAssignInfo"})
    @ApiOperation(value = "跨城合并派单弹窗信息拉取", response = ResponseData.class)
    public ResponseData<List<CrosstownMergeAssignInfoVo>> mergeAssignInfo(HttpServletRequest request, @RequestHeader String appid, @RequestBody MergeAssignParam mergeAssignParam) {
        String driverUuid = getUserUid(request);
        mergeAssignParam.setDriverUuid(driverUuid);
        mergeAssignParam.setAppid(appid);
        return orderJoinService.crosstownAssignInfo(mergeAssignParam);
    }

    @RequestMapping(value = {"/token/crosstown/order/taskList"})
    @ApiOperation(value = "任务列表", response = ResponseData.class)
    public ResponseData<List<CrosstownOrderTaskInfoVo>> taskList(@RequestHeader String appid, @RequestBody CrosstownOrderParam crosstownOrderParam) {
        crosstownOrderParam.setAppid(appid);
        return orderJoinService.taskList(crosstownOrderParam);
    }
}
